package com.ecoolex.discount.sales.api.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import com.ecoolex.discount.sales.api.IpUtils;
import com.ecoolex.discount.sales.pojo.YouWorldOrderRecordRequest;
import com.ecoolex.discount.sales.pojo.YouWorldOrderStatusUpdateRequest;
import com.ecoolex.discount.sales.ws.YouWorldOrderRecordService;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/youworld")
@Slf4j
@CrossOrigin(origins = "*")
public class YouWorldOrderRecordController {


    @Reference
    YouWorldOrderRecordService youWorldOrderRecordService;

    @PostMapping("record")
    public Resp record(@RequestBody YouWorldOrderRecordRequest data, HttpServletRequest request){
        Map<String,String> res = new HashMap<>();
        try {
            String ip = IpUtils.getCliectIp(request);
            youWorldOrderRecordService.record(data,ip);
        }catch (Exception e){
            log.info("youworld-order-record",e);
            return Resp.error(e.getMessage());
        }
        return Resp.ok("ok");
    }

    @PostMapping("update")
    public Resp update(@RequestBody YouWorldOrderStatusUpdateRequest data, HttpServletRequest request){
        Map<String,String> res = new HashMap<>();
        try {
            String ip = IpUtils.getCliectIp(request);
            youWorldOrderRecordService.update(data,ip);
        }catch (Exception e){
            log.info("youworld-order-record",e);
            return Resp.error(e.getMessage());
        }
        return Resp.ok("ok");
    }

    @Data
    public static class Resp {
        private boolean success;
        private String message;
        public static Resp error(String message){
            Resp resp = new Resp();
            resp.setSuccess(false);
            resp.setMessage(message);
            return  resp;
        }
        public static Resp ok(String message){
            Resp resp = new Resp();
            resp.setSuccess(true);
            resp.setMessage(message);
            return  resp;
        }

    }
}
